package self.Lunastic.HexaSweeper.Unit;

import self.Lunastic.HexaSweeper.Manager.GameManager;
import self.Lunastic.HexaSweeper.Manager.StyleOption;

import java.awt.*;
import java.util.ArrayList;

public class ButtonArrow extends ButtonBase {

    // 最大悬停进度
    private final int MAX_HOVERING = 600;
    // 最大点击进度
    private final int MAX_CLICKING = 240;
    // 悬停进度
    private int _hovering = 0;
    // 点击进度
    private int _clicking = 0;
    // 方向
    private final int d;

    public ButtonArrow(int x, int y, int w, int h, int d) {
        super(x, y, w, h);
        this.d = d;
    }

    @Override
    protected void update() {
        this._hovering += GameManager.TPS * (this._hover ? 1 : -1);
        this._hovering = Math.max(0, Math.min(MAX_HOVERING, this._hovering));
        this._clicking += GameManager.TPS * (this._leftPress ? 1 : -1);
        this._clicking = Math.max(0, Math.min(MAX_CLICKING, this._clicking));
    }

    @Override
    protected void extraPaint() {
        int w = this.getWidth();
        int h = this.getHeight();
        int dx = (w/2)*this._hovering /MAX_HOVERING *(MAX_CLICKING-this._clicking) /MAX_CLICKING;
        g2d.setColor(this._leftPress ? StyleOption.ARROW_CLICK : StyleOption.ARROW_COLOR);
        int   nPs = 6;
        int[] xPs = {
                w*5/6   -dx,
                w       -dx,
                w*3/4   -dx,
                w       -dx,
                w*5/6   -dx,
                w/2     -dx,
        };
        int[] yPs = {
                0,
                0,
                h/2,
                h,
                h,
                h/2,
        };
        // 原点旋转
        if(this.d == 2 || this.d == 6) for (int i = 0; i < nPs; i++) {
            xPs[i] = w - xPs[i];
            yPs[i] = h - yPs[i];
        }
        // 左下-右上翻转
        switch (this.d){
            case 2: case 8: g2d.fillPolygon(yPs, xPs, nPs);break;
            case 4: case 6: g2d.fillPolygon(xPs, yPs, nPs);break;
        }
    }
}
